﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace SimpleLibrary
{
    class Logger
    {
        private static TraceSwitch _traceSwitch;
        public static void Init()
        {
            if (File.Exists(".\\OperationLog.log"))
            {
                try
                {
                    File.Delete(".\\OperationLog.log");
                }
                catch { }
            }
            try
            {
                _traceSwitch = new TraceSwitch("Tracer", "Tracer for Application at Runtime.");

                var listener = new TextWriterTraceListener(".\\OperationLog.log");
                Trace.Listeners.Add(listener);
                Trace.AutoFlush = true;
            }
            catch
            {

            }
        }


        public static void WriteTrace(String message, TraceLevel level)
        {
            DateTime dt = DateTime.Now;
            try
            {
                switch (level)
                {
                    case TraceLevel.Off:
                        break;
                    case TraceLevel.Error:
                        Trace.WriteLineIf(_traceSwitch.TraceError, dt + ": " + message);
                        break;
                    case TraceLevel.Warning:
                        Trace.WriteLineIf(_traceSwitch.TraceWarning, dt + ": " + message);
                        break;
                    case TraceLevel.Info:
                        Trace.WriteLineIf(_traceSwitch.TraceInfo, dt + ": " + message);
                        break;
                    case TraceLevel.Verbose:
                        Trace.WriteLineIf(_traceSwitch.TraceVerbose, dt + ": " + message);
                        break;
                }
                Trace.Flush();
            }
            catch
            {

            }

        }

        public static void WriteError(String message, String errorDetails)
        {
            try
            {
                Trace.Indent();
                Trace.WriteLineIf(_traceSwitch.TraceError, String.Format("{2}: {0}  - \n----------------------------\n {1} \n----------------------------\n", message, errorDetails, DateTime.Now));
                Trace.Unindent();
                Trace.Flush();
            }
            catch
            {

            }

        }
    }
}
